package com.example.pma.controllers;

import com.example.pma.dao.ProjectRepository;
import com.example.pma.dao.StudentRepository;
import com.example.pma.dto.ChartData;
import com.example.pma.entities.Project;
import com.example.pma.entities.Student;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

@Controller
public class HomeController {

    @Value("${version}")
    private String version;
    @Autowired
    ProjectRepository projectRepository;
    @Autowired
    StudentRepository studentRepository;
    @GetMapping("/")
    public String displayHome(Model model) throws JsonProcessingException {
        List<Project> projects = projectRepository.findAll();
        model.addAttribute("versionNumber",version);
        model.addAttribute("projects",projects);

        List<ChartData> projectStatusData=projectRepository.getProjectStatus();
        //转换json结构
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonString = objectMapper.writeValueAsString(projectStatusData);
        //
        model.addAttribute("projectStatusData",jsonString);
        List<Student> students = studentRepository.findAll();
        model.addAttribute("students",students);
        return "main/home";

    }

    }

